//
//  TrackSegment.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2018/1/12.
//  Copyright © 2018年 2Bulu. All rights reserved.
//

#import "TrackSegment.h"

@implementation TrackSegment


-(id)initWithData:(NSData *)data
{
    self = [super init];
    if(self)
    {
        _pointsData = data;
        
    }
    return self;
}
-(id)initWithPointsData:(LocationPointType *)dataBuffer pointCount:(size_t)count
{
    self = [super init];
    if(self)
    {
        size_t s = count*sizeof(LocationPointType);
        _pointsData = [[NSData alloc]initWithBytes:dataBuffer length:s];
    }
    
    return self;
}


-(LocationPointsSegmentType)pointsSegment
{
    LocationPointsSegmentType seg;
    seg.dataBuffer = (LocationPointType *)_pointsData.bytes;
    seg.pointsCount = (int)_pointsData.length/sizeof(LocationPointType);
    return seg;
    
}

-(NSUInteger)pointCount
{
    NSUInteger count = _pointsData.length/sizeof(LocationPointType);
    return count;
}

-(CLLocation *)positionAtIndex:(NSUInteger)index
{
    LocationPointType p = [self pointAtIndex:index];
    
    return objcLocationFromPoint(p);
}
-(LocationPointType)pointAtIndex:(NSUInteger)index
{
    LocationPointType p = {};
    LocationPointsSegmentType seg = self.pointsSegment;
    if(index<seg.pointsCount)
    {
        p = self.pointsSegment.dataBuffer[index];
    }
    return p;
}

-(NSArray *)dividAtIndex:(NSUInteger)index
{
    LocationPointsSegmentType seg = self.pointsSegment;
    if(index>=seg.pointsCount)
        return nil;
    
    size_t s1 = sizeof(LocationPointType);
    NSUInteger len = self.pointsData.length;
    
    NSUInteger idx1 = index*s1;
    
    NSData *firstSegData = [self.pointsData subdataWithRange:NSMakeRange(0, idx1+s1)];
    NSData *lastSegData = [self.pointsData subdataWithRange:NSMakeRange(idx1, len-idx1)];
    
    TrackSegment *segObj1 = [[TrackSegment alloc]initWithData:firstSegData];
    TrackSegment *segObj2 = [[TrackSegment alloc]initWithData:lastSegData];
    
    
    return @[segObj1,segObj2];
}



@end
